#-*-coding:utf-8-*-


'''
# ----------------------------------------------------------------------------
#  Purpose:     用于实现外放接口的单元测试

#  Author:      青衫少年春归也

#  Created:     2019.12.23

# ----------------------------------------------------------------------------
'''
import sys
import inspect


#  导入需要进行简单 单元测试的对象
#  要求 具体导入层级到 class类， 或者 function 函数 层级
#  不允许导入 moudle 模块，此类不支持 自动实现单元测试
from handle_files.send_mail import SendEmail
from handle_files.xml_parser import XMLParser
from handle_files.file_upload import UploadFile
from handle_timedata.handle_datetime import HandleTimeData
from handle_files.handle_compress import HandleFileCompress
from performance_analysis.handle_process import HandleProcess



class UnitTest():
    def get_all_decorator_methods(self) -> list:
        """
        获取所有装饰了单测函数（single_class_method_unittest 或 single_function_unittest）的 方法或者函数
        """
        functions = []
        all_moudles = [x for x in dir(sys.modules[__name__]) if not x.startswith("_") and x not in ["sys", "UnitTest", "inspect", "test"]]
        print(f"all_moudles:{all_moudles}")
        for moudle in all_moudles:
            for name, func in inspect.getmembers(eval(moudle)):
                if not name.startswith("_"):
                    last_wrapper = inspect.unwrap(func)
                    if "__unittest_decorator__" in last_wrapper.__name__:
                        functions.append([name, eval(moudle)])
        return functions

    def execution_unittest(self):
        """
        执行简单的单元测试
        """
        functions = self.get_all_decorator_methods()
        for name, moudle in functions:
            try:
                mod = moudle()
                if hasattr(mod, name):
                    res = getattr(mod, name)()
                    if res:
                        print(f"验证函数{name}成功")
                    else:
                        print(f"验证函数{name}失败：返回值 {res}")
            except Exception as error:
                print(f"验证函数{name}失败：{str(error)}")




if __name__ == '__main__':
    test = UnitTest()
    test.execution_unittest()

